package com.shangcheng.search.controller;

import com.github.pagehelper.PageInfo;
import com.shangcheng.goods.feign.BrandFeign;
import com.shangcheng.goods.feign.SpuFeign;
import com.shangcheng.goods.pojo.Brand;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.AbstractDocument;


@Controller
@RequestMapping(value = "/goods")
public class GoodsController {
    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private BrandFeign brandFeign;


    @GetMapping(value = "/list")
    public String GoodsList(@RequestParam int page,
                            @RequestParam int size,
                            ModelMap model) {
        Result brandsR = brandFeign.findAll();
        Result<PageInfo> spuPages = spuFeign.goodsList(page, size);
        model.addAttribute("brandList", brandsR.getData());
        //商品列表数据
        model.addAttribute("goods", spuPages.getData().getList());
        //size
        model.addAttribute("pageSize", spuPages.getData().getPageSize());
        ///上一页
        model.addAttribute("prePage", spuPages.getData().getPrePage());
        //第一页
        model.addAttribute("firstPage", spuPages.getData().getFirstPage());
        //最后一页
        model.addAttribute("lastPage", spuPages.getData().getLastPage());
        //下一页
        model.addAttribute("nextPage", spuPages.getData().getNextPage());
        return "index";
    }

    @GetMapping(value = "/listT")
    public String spuList(@RequestParam int id,
                          @RequestParam int page,
                          @RequestParam int size,
                          ModelMap model) {
        Result<Brand> brand = brandFeign.findById(id);
        Result<PageInfo> spuPage = spuFeign.spuList(id, page, size);
        model.addAttribute("brand", brand.getData());
        //商品列表数据
        model.addAttribute("spu", spuPage.getData().getList());
        //size
        model.addAttribute("pageSize2", spuPage.getData().getPageSize());
        ///上一页
        model.addAttribute("prePage2", spuPage.getData().getPrePage());
        //第一页
        model.addAttribute("firstPage2", spuPage.getData().getFirstPage());
        //最后一页
        model.addAttribute("lastPage2", spuPage.getData().getLastPage());
        //下一页
        model.addAttribute("nextPage2", spuPage.getData().getNextPage());
        return "brand";
    }

    @PostMapping("/addsc")
    public Result addsc(@RequestParam(name = "username") String username,
                        @RequestParam(name = "id") String id) {
        return spuFeign.addsc(username, id);
    }

    @PostMapping("/deletesc")
    public Result deletesc(@RequestParam String id) {
        return spuFeign.deletesc(id);
    }

}
